package Leetcode.MapAndSet.groupAnagrams49;

import java.util.*;

public class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        int len = strs.length;
        if (len == 0) {
            return new ArrayList<>();
        }
        Map<String, List> result = new HashMap<String, List>();
        for (String str : strs) {
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String key = String.valueOf(chars);
            if(!result.containsKey(key)) {
                result.put(key,new ArrayList());
            }
            result.get(key).add(str);
        }
        return new ArrayList(result.values());
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] strings = {"abd","bad","qwe","wqe","qwer","ewqr"};
        System.out.println(solution.groupAnagrams(strings));
    }
}
